#include "Nave.h"

#include "Behavior/setArrive.h"
#include "setNothing.h"
#include "CApp.h"
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>
#include <SDL/SDL_rotozoom.h>

Nave::Nave(float p[3],Character *  arrive, int t){
  tipo = t;
  ready = false;
  follw = 0;
  nochange = false;
  if((Surf= CSurface::OnLoad("images/nave.bmp")) == NULL)
    exit(EXIT_FAILURE);
  CSurface::Transparent(Surf, 0, 183, 239);
  k = new Kinematic();
  k->position[0] = p[0];
  k->position[1] = p[1];
  k->orientation = M_PI*((float) (random()%7))/7;
  // velocidad
  k->max_velocity = 0.04;
  k->max_rotation = 0.04;
  this->width = 108;
  this->height = 70;
  behaviors.push_back(new setNothing());
  behaviors.push_back(new setArrive(this,arrive));
}

/*
 * Pinta al bicho
 */
void Nave::OnRender(SDL_Surface* display){
  CSurface::OnDraw(display, Surf,
    fixX(k->position[0] - width/2), fixY(k->position[1] - height),
    0,70*tipo,108,70);
}

bool Nave::follow(){
  if (follw < 3+tipo*2 && !nochange){
    follw++;
    return true;
  }
  return false;
}


/*
* Aplica comportamiento, actualiza la posicion
*/
void Nave::OnLoop(long time){
  Steering *steering;
  if (follw < 3+tipo*2){
    steering = behaviors[0]->getSteering();
  }else{
    steering = behaviors[1]->getSteering();
    if (steering->linear[0] == steering->linear[1] &&
        steering->linear[1] ==0 && !nochange){
      nochange =true;
      theApp.change();
    }
  }
  steering->linear[2] += -0.008;
  update(steering, time);
}

